<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib uri="/WEB-INF/tlds/template.tld" prefix='template'%>

<script type="text/javascript">

$(document).ready(function(){

	$('#wait').hide();
	
	$("form#login").submit(function(){
		
		$("#reg_button").attr('disabled', true);
		$("#reg_button").attr('value', "Bitte warten...");

		if(checkInput() == 1){
			$("#reg_button").attr('disabled', false);
			$("#reg_button").attr('value', "Login");		
			return false;
		}

		$('#wait').show();
		
		$.post("login.do",{
					email: $("#email").val(),
					password: $("#password").val()
				}, function(xml) {
				handleResponse(xml);
				$("#reg_button").attr('disabled', false);
				$("#reg_button").attr('value', "Login");
				$('#wait').hide();		
		});		

		return false;
	});
});


function handleResponse(xml) {
	
	main = $("main",xml).text();
	if(main != ""){
		window.location.href = "home.jsp";		
		return;
	}
	
	$("#email_msg").empty();
	$("#password_msg").empty();
	
	email = $("email",xml).text();
	password = $("password",xml).text();
	
	$("#email_msg").html(email);
	$("#password_msg").html(password);
}

function checkInput() {

	var email = $("#email").val();
	var passord = $("#password").val();
	var error = 0;

	$("#email_msg").empty();
	$("#password_msg").empty();

	if(email == ""){
		$("#email_msg").html("Deine E-Mail-Addresse ist erforderlich");
		error = 1;
	}
	if(passord == ""){
		$("#password_msg").html("Password ist erforderlich");
		error = 1;
	}
	return error;
}

</script>

<div id="login_content"><c:if test="${requestScope.msg != null}">
	<h1>
		<span style="color: #245C5F">
			<fmt:message key="${requestScope.msg}" />
		</span>
	</h1><br/>
<br/>
</c:if>

<h1>Login</h1>
<br />
<form id="login">

<table cellspacing="3">
	<tr>
		<td width="70px">Email:</td>
		<td><input id="email" name="email" type="text" class="Feld"
			maxlength="40"></td>
		<td><span style="color: red; margin-left: 5px" " id="email_msg"></span></td>
	</tr>
	<tr>
		<td>Passwort:</td>
		<td><input id="password" name="password" type="password"
			class="Feld" maxlength="40"></td>
		<td><span style="color: red; margin-left: 5px" id="password_msg"></span></td>
	</tr>
</table>

<br />
<input id="reg_button" type="submit" value=" Login " class="gwt-Button" />
<img alt="" src="/img/ajax-loader.gif" id="wait" /></form>

</div>
